/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import com.dairy.database.DatabaseInterface;
import com.dairy.helpers.DatabaseHelper;
import com.dairy.structures.Admin;
import com.dairy.structures.Company;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author tecton
 */
public class ViewProductAction extends org.apache.struts.action.Action
{
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        DatabaseInterface db;
        HttpSession session = request.getSession();
        String userType = session.getAttribute("userType").toString();
        if (userType.equals("user"))
        {
            db = DatabaseHelper.getUserDatabase(servlet);
            String company = request.getParameter("id_company");
            String name_category = request.getParameter("name_category");
            Company[] com = db.getWholeCompany();
            String[] cat = db.getWholeCategory();
            if (company == null && name_category == null)
            {
                request.setAttribute("company", com);
                request.setAttribute("category", cat);
            }
            else
            {
                request.setAttribute("company", com);
                request.setAttribute("category", cat);
                int id_company = Integer.parseInt(request.getParameter("id_company"));
                request.setAttribute("product", db.getDairyProduct(id_company, name_category));
            }
        }
        else
        {
            db = DatabaseHelper.getAdminDatabase(servlet);
            String username = session.getAttribute("username").toString();
            String[] cat = db.getWholeCategory();
            Admin admin = db.getAdmin(username);
            String name_category = request.getParameter("name_category");
            if (name_category == null)
            {
                request.setAttribute("category", cat);
            }
            else
            {
                request.setAttribute("category", cat);
                request.setAttribute("product", db.getDairyProduct(admin.id_company, name_category));
            }
        }
        return mapping.findForward(userType);
    }
}
